﻿/****************************** Module Header ******************************\
 * Module Name:  MouseHelper.cs
 * Module Version:  1.0.0 (2012-7-21)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is a basic class for the webpage.  It supplies common public
 * methods to process a internet request, such as Request processing and so on. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Runtime.InteropServices;

namespace UCF.Core.Forms
{
    /// <summary>
    /// 鼠标帮助类
    /// </summary>
    public class MouseHelper
    {
        /// <summary>
        /// 鼠标事件枚举
        /// </summary>
        [Flags]
        public enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        internal const byte SM_CMOUSEBUTTONS = 43;
        internal const byte SM_MOUSEPRESENT = 19;
        internal const byte SM_MOUSEWHEELPRESENT = 75;

        /// <summary>
        /// 获取屏幕宽度
        /// </summary>
        public static int FullScreenPosition_X
        {
            get
            {
                POINTAPI _POINTAPI = new POINTAPI();
                GetCursorPos(ref _POINTAPI);
                return _POINTAPI.x;
            }
        }

        /// <summary>
        /// 获取屏幕高度
        /// </summary>
        public static int FullScreenPosition_Y
        {
            get
            {
                POINTAPI _POINTAPI = new POINTAPI();
                GetCursorPos(ref _POINTAPI);
                return _POINTAPI.y;
            }
        }

        /// <summary>
        /// 过去鼠标事件类型
        /// </summary>
        public static string Type
        {
            get
            {
                if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
                {
                    return "本计算机尚未安装鼠标";
                }
                if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
                {
                    return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
                }
                return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
            }
        }

        /// <summary>
        ///鼠标左右键功能互换
        /// </summary>
        /// <param name="bSwap"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
        public static extern int SwapMouseButton(int bSwap);

        /// <summary>
        /// 鼠标的移动区域限制
        /// 0为释放限制
        /// </summary>
        /// <param name="lpRect"></param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "ClipCursor")]
        public static extern int ClipCursor(ref RECT lpRect);

        /// <summary>
        /// 获取鼠标坐标
        /// </summary>
        /// <param name="lpPoint"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetCursorPos")]
        public static extern int GetCursorPos(ref POINTAPI lpPoint);

        /// <summary>
        /// 显示和隐藏鼠标指针.  
        /// 1为显示0为隐藏 
        /// </summary>
        /// <param name="bShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "ShowCursor")]
        public static extern bool ShowCursor(bool bShow);

        /// <summary>
        /// 将非模态窗口显示为模态窗口
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="fEnable"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "EnableWindow")]
        public static extern int EnableWindow(int hwnd, int fEnable);

        /// <summary>
        /// 获得窗口的大小
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="lpRect"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public static extern int GetWindowRect(int hwnd, ref RECT lpRect);

        /// <summary>
        /// 设置鼠标坐标
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        public static extern int SetCursorPos(int x, int y);

        /// <summary>
        /// 返回Win桌面中各种显示单元的宽度和高度、是否安装了鼠标、是否调换了鼠标左右键的定义等
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int nIndex);

        /// <summary>
        /// 参数wCount，表示鼠标双击时间，为毫秒级,系统默认时间为500
        /// </summary>
        /// <param name="wCount"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
        public static extern int SetDoubleClickTime(int wCount);

        /// <summary>
        /// 该函数无参数；它的返回值为毫秒，为双击鼠标双击有效的时间间隔。
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
        public static extern int GetDoubleClickTime();

        /// <summary>
        /// 只是休眠而已
        /// </summary>
        /// <param name="dwMilliseconds"></param>
        [DllImport("kernel32.DLL", EntryPoint = "Sleep")]
        public static extern void Sleep(int dwMilliseconds);

        /// <summary>
        /// 模拟鼠标操作
        /// 调用方法
        /// mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
        /// mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
        ///    其中X，Y分别是你要点击的点的横坐标和纵坐标 
        /// </summary>
        /// <param name="dwFlags"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="dwData"></param>
        /// <param name="dwExtraInfo"></param>
        [DllImport("user32.dll")]
        public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        /// <summary>
        /// 隐藏鼠标
        /// </summary>
        public static void Hide()
        {
            ShowCursor(false);
        }

        /// <summary>
        /// 显示鼠标
        /// </summary>
        public static void Show()
        {
            ShowCursor(true);
        }

        /// <summary>
        /// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
        /// </summary>
        /// <param name="ObjectForm"></param>
        public static void Lock(System.Windows.Forms.Form ObjectForm)
        {
            RECT _FormRect = new RECT();
            GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
            ClipCursor(ref _FormRect);
        }

        /// <summary>
        /// 解锁鼠标
        /// </summary>
        public static void UnLock()
        {
            RECT _ScreenRect = new RECT();
            _ScreenRect.top = 0;
            _ScreenRect.left = 0;
            _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
            _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
            ClipCursor(ref _ScreenRect);
        }

        /// <summary>
        /// 鼠标失效,不过失效的好像不只是鼠标,小心哦
        /// </summary>
        /// <param name="ObjectForm"></param>
        public static void Disable(System.Windows.Forms.Form ObjectForm)
        {
            EnableWindow(ObjectForm.Handle.ToInt32(), 0);
        }

        /// <summary>
        /// 恢复鼠标功能
        /// </summary>
        /// <param name="ObjectForm"></param>
        public static void Enable(System.Windows.Forms.Form ObjectForm)
        {
            EnableWindow(ObjectForm.Handle.ToInt32(), 1);
        }

        /// <summary>
        /// 鼠标自己移动
        /// </summary>
        /// <param name="From_Handle_ToInt32"></param>
        /// <param name="To_Handle_ToInt32"></param>
        public static void Move(int From_Handle_ToInt32, int To_Handle_ToInt32)
        {
            RECT rectFrom = new RECT();
            RECT rectTo = new RECT();
            int i;
            GetWindowRect(From_Handle_ToInt32, ref rectFrom);
            GetWindowRect(To_Handle_ToInt32, ref rectTo);
            if ((rectFrom.left + rectFrom.right) / 2 - (rectTo.left + rectTo.right) / 2 > 0)
            {
                for (i = (rectFrom.left + rectFrom.right) / 2; i >= (rectTo.left + rectTo.right) / 2; i--)
                {
                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
                    Sleep(1);
                }
            }
            else
            {
                for (i = (rectFrom.left + rectFrom.right) / 2; i <= (rectTo.left + rectTo.right) / 2; i++)
                {
                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom) / 2);
                    Sleep(1);
                }
            }
            if ((rectFrom.top + rectFrom.bottom) / 2 - (rectTo.top + rectTo.bottom) / 2 > 0)
            {
                for (i = (rectFrom.top + rectFrom.bottom) / 2; i >= (rectTo.top + rectTo.bottom) / 2; i--)
                {
                    SetCursorPos((rectTo.left + rectTo.right) / 2, i);
                    Sleep(1);
                }
            }
            else
            {
                for (i = (rectFrom.top + rectFrom.bottom) / 2; i <= (rectTo.top + rectTo.bottom) / 2; i++)
                {
                    SetCursorPos((rectTo.left + rectTo.right) / 2, i);
                    Sleep(1);
                }
            }
        }

        // 得到你的鼠标类型

        /// <summary>
        /// 设置鼠标双击时间
        /// </summary>
        /// <param name="MouseDoubleClickTime"></param>
        public static void DoubleClickTime_Set(int MouseDoubleClickTime)
        {
            SetDoubleClickTime(MouseDoubleClickTime);
        }

        /// <summary>
        /// 获取鼠标双击时间
        /// </summary>
        /// <returns></returns>
        public static string DoubleClickTime_Get()
        {
            return GetDoubleClickTime().ToString();
        }

        /// <summary>
        /// 设置鼠标默认主键
        /// </summary>
        public static void DefaultRightButton()
        {
            SwapMouseButton(1);
        }

        /// <summary>
        /// 获取鼠标左键
        /// </summary>
        public static void DefaultLeftButton()
        {
            SwapMouseButton(0);
        }

        /// <summary>
        /// 鼠标坐标
        /// </summary>
        public struct POINTAPI
        {
            public int x;
            public int y;
        }

        /// <summary>
        /// 鼠标移动范围
        /// </summary>
        public struct RECT
        {
            public int bottom;
            public int left;
            public int right;
            public int top;
        }

    }
}